<?php
use yii\helpers\Html;
use yii\helpers\Url;

/**
 * @var $this \yii\web\View
 */

$controller = Yii::$app->controller;
?>
<div id="sidebar" class="sidebar responsive">
    <!--
    <div class="sidebar-shortcuts" id="sidebar-shortcuts">
        <div class="sidebar-shortcuts-large" id="sidebar-shortcuts-large">
            <?php echo Html::a('<i class="ace-icon fa fa-plus"></i>', ['/'], ['class' => 'btn btn-success']);?>
            <?php echo Html::a('<i class="ace-icon fa fa-book"></i>', ['/'], ['class' => 'btn btn-info']);?>
            <?php echo Html::a('<i class="ace-icon fa fa-users"></i>', ['/user/admin/index'], ['class' => 'btn btn-warning']);?>
            <?php echo Html::a('<i class="ace-icon fa fa-signal"></i>', ['/'], ['class' => 'btn btn-danger']);?>
        </div>
        <div class="sidebar-shortcuts-mini" id="sidebar-shortcuts-mini">
            <?php echo Html::a('', ['/'], ['class' => 'btn btn-success']);?>
            <?php echo Html::a('', ['/'], ['class' => 'btn btn-info']);?>
            <?php echo Html::a('', ['/user/admin/index'], ['class' => 'btn btn-warning']);?>
            <?php echo Html::a('', ['/'], ['class' => 'btn btn-danger']);?>
        </div>
    </div>
    -->

    <ul class="nav nav-list">
        <li class="">
            <a href="<?php echo Url::to(['/admin/default/index']);?>">
                <i class="menu-icon fa fa-tachometer"></i>
                <span class="menu-text"> 控制台</span>
            </a>
            <b class="arrow"></b>
        </li>
        <?php
        /**
         * 检查菜单是否需要设置为当前状态
         * @param string $route
         * @return boolean
         */
        function checkMenuActive($route) {
            $controller = Yii::$app->controller;
            if (!$controller) {
                return false;
            }
            if ($controller->module->id != 'admin') {
                return false;
            }
            $route = preg_split('/\//', $route);
            if (count($route) == 1) {
                if ($controller->id == $route[0]) {
                    return true;
                }
            } elseif (count($route) == 2) {
                if ($controller->id == $route[0] && $controller->action->id == $route[1]) {
                    return true;
                }
            }
            return false;
        }?>
        <?php $items = [
            ['label'=>'权限管理', 'rbac'=>'rbac/menu', 'icon'=>'fa fa-lock', 'active'=>checkMenuActive('rbac'), 'items'=>[
                ['label'=>'管理员列表', 'rbac'=>'rbac/manager', 'url'=>['/admin/rbac/manager'], 'active'=>checkMenuActive('rbac/manager')],
                ['label'=>'管理日志', 'rbac'=>'rbac/log', 'url'=>['/admin/rbac/log'], 'active'=>checkMenuActive('rbac/log')],
                ['label'=>'管理角色列表', 'rbac'=>'rbac/role', 'url'=>['/admin/rbac/role'], 'active'=>checkMenuActive('rbac/role')],
                ['label'=>'权限列表', 'rbac'=>'rbac/item', 'url'=>['/admin/rbac/item'], 'active'=>checkMenuActive('rbac/item')],
            ]],
            ['label'=>'数据管理', 'rbac'=>'db/menu', 'icon'=>'fa fa-database', 'active'=>checkMenuActive('db'), 'items'=>[
                ['label'=>'SQL查询', 'rbac'=>'db/sql', 'url'=>['/admin/db/sql'], 'active'=>checkMenuActive('db/sql')],
                ['label'=>'数据库备份', 'rbac'=>'db/backup', 'url'=>['/admin/db/backup'], 'active'=>checkMenuActive('db/backup')],
            ]],
            ['label'=>'用户管理', 'rbac'=>'user/menu', 'icon'=>'fa fa-users', 'active'=>checkMenuActive('user'), 'items'=>[
                ['label'=>'用户列表', 'rbac'=>'user/list', 'url'=>['/admin/user/list'], 'active'=>checkMenuActive('user/list')],
            ]],
            ['label'=>'广告管理', 'rbac'=>'ad/menu', 'icon'=>'fa fa-paper-plane', 'active'=>checkMenuActive('ad'), 'items'=>[
                ['label'=>'广告列表', 'rbac'=>'ad/list', 'url'=>['/admin/ad/list'], 'active'=>checkMenuActive('ad/list')],
                ['label'=>'广告位置', 'rbac'=>'ad/location', 'url'=>['/admin/ad/location'], 'active'=>checkMenuActive('ad/location')],
            ]],
            ['label'=>'资讯管理', 'rbac'=>'article/menu', 'icon'=>'fa fa-newspaper-o', 'active'=>checkMenuActive('article'), 'items'=>[
                ['label'=>'资讯列表', 'rbac'=>'article/list', 'url'=>['/admin/article/list'], 'active'=>checkMenuActive('article/list')],
                ['label'=>'资讯分类', 'rbac'=>'article/category', 'url'=>['/admin/article/category'], 'active'=>checkMenuActive('article/category')],
                ['label'=>'资讯审核', 'rbac'=>'article/verify', 'url'=>['/admin/article/verify'], 'active'=>checkMenuActive('article/verify')],
            ]],
            ['label'=>'统计分析', 'rbac'=>'statistics/menu', 'icon'=>'fa fa-line-chart', 'active'=>checkMenuActive('statistics'), 'items'=>[
                ['label'=>'用户统计', 'rbac'=>'statistics/user', 'url'=>['/admin/statistics/user'], 'active'=>checkMenuActive('statistics/user')],
                ['label'=>'资讯访问统计', 'rbac'=>'statistics/article', 'url'=>['/admin/statistics/article'], 'active'=>checkMenuActive('statistics/article')],
            ]],
            ['label'=>'系统管理', 'rbac'=>'system/menu', 'icon'=>'fa fa-cogs', 'active'=>checkMenuActive('system'), 'items'=>[
                ['label'=>'系统设置', 'rbac'=>'system/config', 'url'=>['/admin/system/config'], 'active'=>checkMenuActive('system/config')],
                ['label'=>'群发消息', 'rbac'=>'system/message', 'url'=>['/admin/system/message'], 'active'=>checkMenuActive('system/message')],
                ['label'=>'定时任务', 'rbac'=>'system/task', 'url'=>['/admin/system/task'], 'active'=>checkMenuActive('system/task')],
            ]],
        ];?>
        <?php foreach ($items as $item) {
            if (!Yii::$app->manager->can($item['rbac'])) {
                continue;
            }?>
            <li class="<?php if ($item['active']) {echo 'open active';}?>">
                <a href="#" class="dropdown-toggle">
                    <i class="menu-icon <?php echo $item['icon'];?>"></i>
                    <span class="menu-text"><?php echo $item['label'];?></span>
                    <b class="arrow fa fa-angle-down"></b>
                </a>
                <b class="arrow"></b>
                <ul class="submenu">
                    <?php foreach ($item['items'] as $subitem) {
                        if (!Yii::$app->manager->can($subitem['rbac'])) {
                            continue;
                        }?>
                        <li class="<?php if ($subitem['active']) {echo 'active';}?>">
                            <a href="<?php echo Url::to($subitem['url']);?>">
                                <i class="menu-icon fa fa-caret-right"></i>
                                <?php echo $subitem['label'];?>
                            </a>
                            <b class="arrow"></b>
                        </li>
                    <?php }?>
                </ul>
            </li>
        <?php }?>
    </ul>
    <div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
        <i class="ace-icon fa fa-angle-double-left" data-icon1="ace-icon fa fa-angle-double-left" data-icon2="ace-icon fa fa-angle-double-right"></i>
    </div>
    <script>
        try {
            ace.settings.check('sidebar', 'collapsed');
        } catch (e) {
        }
    </script>
</div>
